#include "event_handler.h"
#include "main.h"
#include "settings.h"

static uint16_t counter_event_10us = 0;
static uint16_t counter_event_1ms = 0;

struct_event_status event_status;

void event_handler_execute(void) {
  if (event_status.flags & (1<<EVENT_HANDLER_CHECK_PTT_INPUTS)) {
    if (settings_get_ptt_input_enabled() & (1<<SETTING_PTT_INPUT_FOOTSWITCH)) {
      event_status.temp_ptt_input = ((LPC_GPIO3->FIOPIN & (1<<26)) >> 26);

      if (settings_get_ptt_input_invert() & (1<<SETTING_PTT_INPUT_FOOTSWITCH)) {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_FOOTSWITCH);
        else
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_FOOTSWITCH);
      }
      else {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_FOOTSWITCH);
        else
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_FOOTSWITCH);
      }
    }

    if (settings_get_ptt_input_enabled() & (1<<SETTING_PTT_INPUT_FSKCW_RTS)) {
      event_status.temp_ptt_input = ((LPC_GPIO2->FIOPIN & (1<<13)) >> 13);

      if (settings_get_ptt_input_invert() & (1<<SETTING_PTT_INPUT_FSKCW_RTS)) {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_FSKCW_RTS);
        else
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_FSKCW_RTS);
      }
      else {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_FSKCW_RTS);
        else
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_FSKCW_RTS);
      }
    }

    if (settings_get_ptt_input_enabled() & (1<<SETTING_PTT_INPUT_WK_RTS)) {
      event_status.temp_ptt_input = ((LPC_GPIO1->FIOPIN & (1<<16)) >> 16);

      if (settings_get_ptt_input_invert() & (1<<SETTING_PTT_INPUT_WK_RTS)) {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_WK_RTS);
        else
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_WK_RTS);
      }
      else {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_WK_RTS);
        else
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_WK_RTS);
      }
    }

    if (settings_get_ptt_input_enabled() & (1<<SETTING_PTT_INPUT_WK_DTR)) {
      event_status.temp_ptt_input = ((LPC_GPIO1->FIOPIN & (1<<17)) >> 17);

      if (settings_get_ptt_input_invert() & (1<<SETTING_PTT_INPUT_WK_DTR)) {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_WK_DTR);
        else
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_WK_DTR);
      }
      else {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_WK_DTR);
        else
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_WK_DTR);
      }
    }

    if (settings_get_ptt_input_enabled() & (1<<SETTING_PTT_INPUT_VOX_SENSE)) {
      event_status.temp_ptt_input = 0;//((LPC_GPIO1->FIOPIN & (1<<17)) >> 17);

      if (settings_get_ptt_input_invert() & (1<<SETTING_PTT_INPUT_VOX_SENSE)) {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_VOX_SENSE);
        else
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_VOX_SENSE);
      }
      else {
        if (event_status.temp_ptt_input)
          event_status.curr_ptt_input &= ~(1<<STATUS_PTT_INPUT_VOX_SENSE);
        else
          event_status.curr_ptt_input |= (1<<STATUS_PTT_INPUT_VOX_SENSE);
      }
    }

    if (event_status.curr_ptt_input != event_status.prev_ptt_input) {
      status_set_ptt_input_state(event_status.curr_ptt_input);

      event_status.prev_ptt_input = event_status.curr_ptt_input;
    }

    event_status.flags &= ~(1<<EVENT_HANDLER_CHECK_PTT_INPUTS);
  }
}

void event_handler_1ms_tick(void) {
  counter_event_1ms++;
}

void event_handler_10us_tick(void) {
  if ((counter_event_10us % EVENT_HANDLER_POLL_PTT_STATUS_INTERVAL) == 0) {
    event_status.flags |= (1<<EVENT_HANDLER_CHECK_PTT_INPUTS);
  }

  counter_event_10us++;
}
